<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="food">
        <label>食物<input type="checkbox" name="" id="" onclick="c()"></label>
        <label>食物<input type="checkbox" name="" id="" onclick="c()"></label>
        <label>食物<input type="checkbox" name="" id="" onclick="c()"></label>
        <label>食物<input type="checkbox" name="" id="" onclick="c()"></label>
        <label>食物<input type="checkbox" name="" id="" onclick="c()"></label>
        <label>食物<input type="checkbox" name="" id="" onclick="c()"></label>
        <label>食物<input type="checkbox" name="" id="" onclick="c()"></label>
    </div>
    <label>
        全选/全不选
        <input type="checkbox" name="" id="all" onclick="a(this)">
    </label>
    <button onclick="b()">
        反选
    </button>
    <script>
        var food = document.querySelectorAll('.food input');
        var foods = Array.from(food);
        var all = document.getElementById('all');

        function a(that) {
            for (var i = 0; i < food.length; i++) {
                food[i].checked = that.checked;
            }
        }

        function b() {
            for (var i = 0; i < food.length; i++) {
                food[i].checked = !food[i].checked;
            }
            c();
        }

        function c() {
            var a = foods.every(function(value) {
                return value.checked;
            });
            if (a) {
                all.checked = true;
            } else {
                all.checked = false;
            }
        }
    </script>
</body>

</html>